﻿using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using YanhuaMMO;
/// <summary>
/// 时间格式化
/// </summary>
public class TimeFormat : MonoBehaviour {



    private TextMeshProUGUI m_MeshText;
    private bool b_IsDown = false;
    private string s_EndTime;
    private string s_Str;
    private float m_Totaltime = 0;
    private float m_CacheTime = 0;

    private string s_markTime;
    private string s_intervaltime;

    private int m_MethodIndex = 0;
    //public string m_luaScriptName;
    void Start () {

    }

    

    //设置倒计时 diedTime不需要计算
    public void OnSetTextTimeFormat(string str, string endTime,bool isDown )
    {
        m_MethodIndex = 1;
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }

        s_EndTime = endTime;
        s_Str = str;

        long jsTimeStamp = long.Parse(endTime);
        long now = GetDateLong(DateTime.Now);

        long dy = (jsTimeStamp-now) / 1000;
        if (dy<=0)
        {
            m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
          str, 0, 0, 0);
            b_IsDown = false;
            //Util.CallMethod(m_luaScriptName, "OnTimerFinish");
        
        }
        else
        {
            int hour = (int)(dy / (60 * 60));
            int minute = (int)(dy - hour * 3600) / 60;
            int Second = (int)(dy - 3600 * hour - minute * 60);
            m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
          str, hour, minute, Second);
            b_IsDown = isDown;
            
        }
    }
    //设置倒计时 四参数 diedTime 需要计算 
    public void OnSetTextTimeFormat(string str, string marktime,string intervaltime,bool isDown )
    {
        m_MethodIndex = 2;
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }

        s_markTime = marktime;
        s_intervaltime = intervaltime;

        long markTime = long.Parse(marktime);
        long itime = long.Parse(intervaltime);
        s_Str = str;

        long jsTimeStamp = markTime + itime;
        long now = GetDateLong(DateTime.Now);

        long dy = (jsTimeStamp - now) / 1000;
        if (dy <= 0)
        {
            m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
          str, 0, 0, 0);
            b_IsDown = false;
            //Util.CallMethod(m_luaScriptName, "OnTimerFinish");
        }
        else
        {
            int hour = (int)(dy / (60 * 60));
            int minute = (int)(dy - hour * 3600) / 60;
            int Second = (int)(dy - 3600 * hour - minute * 60);
            m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
          str, hour, minute, Second);
            b_IsDown = isDown;
        }
    }

    //设置到期时间
    public void OnSetTextEndTime(string str, string endTime)
    {
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }

        long endtime = long.Parse(endTime);
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        DateTime dt = startTime.AddMilliseconds(endtime);
        m_MeshText.text = string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}", dt);
    }

    //设置历史时间
    public void OnSetTextHistoryTime(string str,string beginTime)
    {
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }
        long jsTimeStamp = long.Parse(beginTime);
        long now = GetDateLong(DateTime.Now);

        long dy = (now - jsTimeStamp) / 1000;
        int hour = (int)(dy / (60 * 60));
        int minute = (int)(dy - hour * 3600) / 60;
        int Second = (int)(dy - 3600 * hour - minute * 60);
        m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
        str, hour, minute, Second);
    }


    private static long GetDateLong(object time)
    {
        DateTime epoc = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        TimeSpan delta = new TimeSpan();

        if (time is DateTime)
            delta = ((DateTime)time).Subtract(epoc);

        else if (time is string)
            delta = DateTime.Parse(time.ToString()).Subtract(epoc);

        else
            throw new ArgumentOutOfRangeException("时间格式错误.1");

        if (delta.TotalMilliseconds < 0)
            throw new ArgumentOutOfRangeException("时间格式错误.2");

        long ticks = (long)delta.TotalMilliseconds;
        return ticks;
    }

    //设置剩余时间
    public void OnSetRemindTime(string str, string remindTime)
    {
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }
        if (remindTime.Equals("0"))
        {
            m_MeshText.text = str + "永久";
            return;
        }

        long end = long.Parse(remindTime);
        long now = GetDateLong(DateTime.Now);
        long dy = (end - now) / 1000;
        int day = (int)(dy / (60 * 60)) / 24;
        int hour = (int)(dy / (60 * 60))%24;
        int longhour = (int)(dy / (60 * 60));
        int minute = (int)(dy - longhour * 3600) / 60;
        int Second = (int)(dy - 3600 * longhour - minute * 60);
        //m_MeshText.text = string.Format("{0:G}{1:D2}:{2:D2}:{3:D2}",
        //str, hour, minute, Second);
        m_MeshText.text = str + day + "天" + hour + "小时" + minute + "分";
    }

    //显示日期
    public void OnSetSignDate(int month, int day)
    {
        if (m_MeshText == null)
        {
            m_MeshText = this.GetComponent<TextMeshProUGUI>();
        }
        int days = System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(DateTime.Now.Year, month);
        if (day > days)
        {
            day = day - days;
            if (month == 12)
            {
                month = 1;
            }
            else
            {
                month++;
            }
            OnSetSignDate(month, day);
        }
        m_MeshText.text = month + "月" + day + "日";
    }

    

    private void OnDisable()
    {
        b_IsDown = false;
    }

    void Update () {

        if (b_IsDown)
        {
            m_CacheTime = m_Totaltime;
            m_Totaltime += Time.deltaTime;
            if ((int)m_Totaltime >= 1+(int)m_CacheTime)
            {
                m_Totaltime = 0;
                if (m_MethodIndex == 1)
                {
                    OnSetTextTimeFormat(s_Str, s_EndTime, b_IsDown);
                }
                else if (m_MethodIndex == 2)
                {
                    OnSetTextTimeFormat(s_Str, s_markTime,s_intervaltime, b_IsDown);
                }
                
            }
        }
		
	}
}
